/**************** (c) 2018  BJB **********************
PROJECT			:	
COMPILER		:	
MODULE			:	ADC.c    
VERSION			:	1.0

CREATION DATE	:	2018-07-01
AUTHOR			:   SIMAXING
******************************************************************************/
#include 	"r_cg_macrodriver.h"
#include 	"r_cg_adc.h"

#include	"Temperature.h"
#include	"Timer.h"        
#include	"ADC.h"        

volatile	u8		ADCDealStatus	= 0;
volatile	u8		TempAD			= 0;
volatile	u8		SeatAD			= 0;
volatile	u16		TempADSum		= 0;
volatile	u16		SeatADSum		= 0;

volatile	u8		Temperature		= 25;
volatile	u8		SeatTemp		= 25;

/*-----------------------------------------------------------------------------
ROUTINE Name :  ADC DEAL INIT
Description :  
-----------------------------------------------------------------------------*/ 
void	ADC_Deal_Init (void)
{     
	ADC_Set_Chanel(CONVERT_AIN21);
}

void	ADC_Set_Chanel	(u8 Channel)	
{
	ADS	= Channel;					
}

void	ADC_Deal (void) 
{
	if (0	== ADCTimeDelay)
	{     
		u8	temp;
		ADCTimeDelay++;
		switch (ADCDealStatus)
		{
			case	0x00:
				ADCDealStatus++;
				break;
			case	0x01:
			case	0x03:
				R_ADC_Start	();
				ADCDealStatus++;
				break;
			case	0x02:
				if (ADCS 	== 0)
				{
					temp				= ADCRH;
					if (0 == TempADSum)	TempADSum = (u16)temp * 0x10;
					TempADSum 			-= (TempADSum >> 4);
					TempADSum 			+= temp;
					TempAD	 			= (u8)(TempADSum >> 4);
					Temperature			= Get_Temperature10K_3435K(TempAD);
					ADC_Set_Chanel(CONVERT_AIN0);
					ADCDealStatus++;
				}
				break;
			case	0x04:
				if (ADCS	== 0)
				{
					if (0 == SeatADSum)	SeatADSum = (u16)temp * 0x10;
					temp				= ADCRH;
					SeatADSum 			-= (SeatADSum >> 4);
					SeatADSum 			+= temp;
					SeatAD	 			= (u8)(SeatADSum >> 4);
					SeatTemp			= Get_Temperature10K_3435K(SeatAD);
					ADC_Set_Chanel(CONVERT_AIN21);
					ADCDealStatus		= 0;
				}
				break;
			default:
				ADCDealStatus = 0;
				break;
		}
	}
}        

/*** (c) 2018  BJB ****************** END OF FILE ***/
